/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.helwen.restaurantbooking.ui;

import com.helwen.restaurantbooking.bo.ITableBo;
import com.helwen.restaurantbooking.datalayer.entities.TblBooking;
import com.helwen.restaurantbooking.helper.BeanManagerHelper;
import com.helwen.restaurantbooking.ui.customcomponent.TableComponent;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

/**
 *
 * @author Nghiata
 */
public class Area extends JPanel {

    private static final ITableBo TABLE_BO = (ITableBo) BeanManagerHelper.getBean("tableBo");
    private GridBagLayout listTablePanelLayout;
    private GridBagConstraints constrains;
    private boolean isNonSmokingArea;
    private JPanel listTablePanel;
    private ReservationPanel editPanel;
    private Image nonSmokingBG = new ImageIcon(this.getClass().getResource("/icons/nonsmokingbg.jpg")).getImage();
    private Image smokingBG = new ImageIcon(this.getClass().getResource("/icons/smokingbg.jpg")).getImage();
    private List<TableComponent> tableComponents;

    public Area() {
        initNonSmokingArea();
    }

    public Area(boolean isNonSmokingArea) {
        this.isNonSmokingArea = isNonSmokingArea;
        tableComponents = TABLE_BO.getAllTable();
        initLayout(isNonSmokingArea);

    }

    private void initLayout(boolean isNonSmokingArea) {
        if (isNonSmokingArea) {

            listTablePanel = new JPanel() {
                @Override
                protected void paintComponent(Graphics g) {
//                    g.drawImage(nonSmokingBG, 0, 0, null);
                    super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
                }
            };
            initNonSmokingArea();
        } else {

            listTablePanel = new JPanel() {
                @Override
                protected void paintComponent(Graphics g) {
//                    g.drawImage(smokingBG, 0, 0, null);
                    super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
                }
            };
            initSmokingArea();
        }

        this.setLayout(new BorderLayout());
        editPanel = new ReservationPanel(true);
        editPanel.setParentComponent(this);
        listTablePanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
        this.add(listTablePanel, BorderLayout.CENTER);
        this.add(editPanel, BorderLayout.EAST);
        editPanel.setVisible(false);

    }

    private void initNonSmokingArea() {

        listTablePanel.setOpaque(false);
        listTablePanelLayout = new GridBagLayout();
        listTablePanel.setLayout(listTablePanelLayout);
        constrains = new GridBagConstraints();

        for (int i = 0; i < 5; i++) {
            final TableComponent tblCom = tableComponents.get(i);
            tableComponents.get(i).addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    tableClick(tblCom.getTableNumber());
                }
            });
            if (i == 0) {
                constrains.gridx = 0;
                constrains.gridy = 0;
                constrains.insets = new Insets(25, 25, 25, 25);
                listTablePanel.add(tblCom, constrains);
                listTablePanel.add(new Label(tblCom.getTableNumber() + ""), constrains);
            }
            if (i == 1) {
                constrains.gridx = 0;
                constrains.gridy = 5;
                listTablePanel.add(tblCom, constrains);
                listTablePanel.add(new Label(tblCom.getTableNumber() + ""), constrains);
            }
            if (i == 2) {
                constrains.gridx = 5;
                constrains.gridy = 0;
                listTablePanel.add(tblCom, constrains);
                listTablePanel.add(new Label(tblCom.getTableNumber() + ""), constrains);
            }
            if (i == 3) {
                constrains.gridx = 5;
                constrains.gridy = 5;
                listTablePanel.add(tblCom, constrains);
                listTablePanel.add(new Label(tblCom.getTableNumber() + ""), constrains);
            }
            if (i == 4) {
                constrains.gridx = 3;
                constrains.gridy = 3;
                listTablePanel.add(tblCom, constrains);
                listTablePanel.add(new Label(tblCom.getTableNumber() + ""), constrains);
            }

        }

    }

    private void initSmokingArea() {

        listTablePanel.setOpaque(false);
        listTablePanelLayout = new GridBagLayout();
        listTablePanel.setLayout(listTablePanelLayout);
        constrains = new GridBagConstraints();
        for (int i = 5; i < 10; i++) {
            final TableComponent tblCom = tableComponents.get(i);
            tableComponents.get(i).addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    tableClick(tblCom.getTableNumber());
                }
            });
            if (i == 5) {
                constrains.gridx = 0;
                constrains.gridy = 0;
                constrains.insets = new Insets(25, 25, 25, 25);
                listTablePanel.add(tblCom, constrains);
                listTablePanel.add(new Label(tblCom.getTableNumber() - 5 + ""), constrains);
            }
            if (i == 6) {
                constrains.gridx = 0;
                constrains.gridy = 5;
                listTablePanel.add(tblCom, constrains);
                listTablePanel.add(new Label(tblCom.getTableNumber() - 5 + ""), constrains);
            }
            if (i == 7) {
                constrains.gridx = 5;
                constrains.gridy = 0;
                listTablePanel.add(tblCom, constrains);
                listTablePanel.add(new Label(tblCom.getTableNumber() - 5 + ""), constrains);
            }
            if (i == 8) {
                constrains.gridx = 5;
                constrains.gridy = 5;
                listTablePanel.add(tblCom, constrains);
                listTablePanel.add(new Label(tblCom.getTableNumber() - 5 + ""), constrains);
            }
            if (i == 9) {
                constrains.gridx = 3;
                constrains.gridy = 3;
                listTablePanel.add(tblCom, constrains);
                listTablePanel.add(new Label(tblCom.getTableNumber() - 5 + ""), constrains);
            }

        }

    }

    public boolean isIsNonSmokingArea() {
        return isNonSmokingArea;
    }

    public void setIsNonSmokingArea(boolean isNonSmokingArea) {
        this.isNonSmokingArea = isNonSmokingArea;
    }

    private void tableClick(Integer number) {
        System.out.println(number);
        TblBooking tblBooking = TABLE_BO.getYellowRedTableBookingInfo(number);
//        if (tblBooking != null) {
        editPanel.tblBooking = tblBooking;
        editPanel.setVisible(true);
        editPanel.setTableNumberSelected(number);
        editPanel.refreshUI();

//        } else {
//         editPanel.resetUI();
//        }
    }

    public void resetListTable() {
        tableComponents = TABLE_BO.getAllTable();
        listTablePanel.removeAll();
        if (isNonSmokingArea) {
            initNonSmokingArea();
        } else {
            initSmokingArea();
        }
        listTablePanel.updateUI();
        editPanel.setVisible(false);

    }
    
}
